package com.yssoft.controller.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.core.TAccounts;
import com.yssoft.entity.core.TPersonnel;
import com.yssoft.entity.core.TRoles;
import com.yssoft.service.core.AccountsService;
import com.yssoft.service.core.PersonService;
import com.yssoft.service.core.RoleService;
import com.yssoft.utils.UserHelper;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;

/**
 * @author 陈翼东
 * @说明 账号管理
 * @time 2018/4/26 下午 04:21
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    
    @Resource
    private AccountsService accountsService;
    
    @Resource
    private PersonService personService;
    
    @Resource
    private RoleService roleService;
    
    /**
     * 跳转到账号管理页面
     *
     * @return
     */
    @RequestMapping("accountPage")
    public String accountPage() {
        return "core/account/accountList";
    }
    
    /**
     * 分页查询该企业的账号信息
     *
     * @param page
     * @param limit
     * @param personName
     * @param loginName
     * @return
     */
    @RequestMapping("queryAccountPage")
    @ResponseBody
    public JSONObject queryAccountPage(int page, int limit, String personName, String loginName) {
        JSONObject jsonObject = accountsService.queryAccountPage(page, limit, personName, loginName);
        return jsonObject;
    }
    
    
    /**
     * 查询当前企业的人员信息
     *
     * @return
     */
    @RequestMapping("queryEntPerson")
    @ResponseBody
    public JSONObject queryEntPerson(int page, int limit) {
        return accountsService.queryCurrentEntPersonPage(page, limit);
    }
    
    
    /**
     * 删除多个账号
     *
     * @param ids
     * @return
     */
    @RequestMapping("delAccounts")
    @ResponseBody
    public Integer delAccounts(String ids) {
        return accountsService.delAccountByIds(ids);
    }
    
    /**
     * 删除单个账号
     *
     * @param id
     * @return
     */
    @RequestMapping("delAccount")
    @ResponseBody
    public Integer delAccount(String id) {
        return accountsService.delAccountById(id);
    }
    
    /**
     * 跳转到编辑页面
     *
     * @param id
     * @param look
     * @return
     */
    @RequestMapping("accountEditPage")
    public String editPage(String id, String look, Model model) {
        List<TRoles> roles=roleService.queryRoleByCompany(UserHelper.getCurrentCompanyId());//查询我的企业有的权限
        model.addAttribute("roles", roles);
        if (id != null && id.trim().length() > 0) {
            //修改
            TAccounts accounts = accountsService.queryAccById(Integer.parseInt(id));
            if (accounts != null && accounts.gettPId()!=null) {
                TPersonnel personnel = personService.queryPersonById(accounts.gettPId() + "");
                model.addAttribute("personnel", personnel);
                //查询当前账户有的权限
               List<TRoles> myRoles=roleService.queryRolesAndRoleByAccount(accounts.getId());
                model.addAttribute("myRoles", myRoles);
                roles.removeAll(myRoles);
                model.addAttribute("roles",roles);
            }
            model.addAttribute("account", accounts);
        }
        model.addAttribute("look", look);
        return "core/account/accountEdit";
    }
    
    
    /**
     * 保存数据信息
     *
     * @param accounts 账号实体
     * @return
     */
    @RequestMapping("saveAccount")
    public String saveAccount(TAccounts accounts,String id,String[] roles) {
        if (id != null && id.trim().length()>0) {
            //修改
            int result = accountsService.updateAccountByIdAndRole(accounts,roles);
        } else {
            accountsService.addAcc(accounts,roles);
        }
        return "redirect:accountPage";
    }
    
    /**
     * 账号和人员解除绑定
     *
     * @param id
     * @return
     */
    @RequestMapping("relievePeron")
    @ResponseBody
    public Integer relievePeron(String id) {
        return accountsService.updateRelievePeron(id);
    }
    
    
    /**
     * 验证账号是否已经注册
     * @param loginName
     * @return
     */
    @RequestMapping("queryAccountRepeat")
    @ResponseBody
    public Integer queryAccountRepeat(String loginName) {
        return accountsService.queryAccountRepeat(loginName);
    }
    
    /**
     * 清空账号的密码
     * @param id
     * @return
     */
    @RequestMapping("accountEmpty")
    @ResponseBody
    public Integer accountEmpty(String id) {
        return accountsService.accountEmpty(id);
    }
    
}
